package com.kcl.dfss.fragments;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.videolan.libvlc.IVideoPlayer;

import com.kcl.dfss.AjustActivity;
import com.kcl.dfss.R;
import com.kcl.dfss.view.CameraView;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SurveillanceFragment extends Fragment implements IVideoPlayer,OnClickListener {

	private CameraView mCameraView;
	private TextView mSurveilance,mRealtime;
	private LinearLayout mRealtimeLayout;
	private FrameLayout jiankong_view;
	private ImageView mAjustView;
	private ImageView mHistoryView;
	private int mVideoWidth = 0;
	private int mVideoHeight = 0;
	private boolean mShowSurveilance = true;
	private String TAG = "SurveillanceFragment";
	//private String mRtspPath = "rtsp://10.0.0.43:8554/test";
	private String mRtspPath = "rtsp://192.168.1.1/HI351XX_VGA";
	private String sLocation = "rtsp://218.204.223.237:554/live/1/66251FC11353191F/e7ooqwcfbqjoo80j.sdp";
	
	
	private Handler mHandler = new Handler(){
		public void handleMessage(Message msg){
			Log.d(TAG,"mVideoWidth is "+mVideoWidth + " mVideoheight is "+mVideoHeight);
			mCameraView.setSurfaceSize(mVideoWidth, mVideoHeight);
		}
	};
	
	private static SurveillanceFragment mInstance = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.layout_surveilance, null);
		
		mCameraView = (CameraView) view.findViewById(R.id.cameraview);
		mCameraView.setVedioPlayerCallback(this);
		
		mSurveilance = (TextView) view.findViewById(R.id.surveilance);
		mRealtime = (TextView) view.findViewById(R.id.realtime);
		mSurveilance.setOnClickListener(this);
		mRealtime.setOnClickListener(this);
		
		mRealtimeLayout = (LinearLayout) view.findViewById(R.id.realtimelayout); 
		jiankong_view =  (FrameLayout)view.findViewById(R.id.jiankong_view);
		
		mCameraView.setRTSPPathAndPlay(sLocation);
		
		mAjustView = (ImageView)view.findViewById(R.id.camera_adjust);
		mAjustView.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String path = mCameraView.snapShot();
				Intent intent = new Intent();
				intent.setClass(getActivity(), AjustActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				Log.d(TAG,"bitmap path is "+path);
				intent.putExtra("bitmap", path);
				startActivity(intent);
			}
		});
		
		mHistoryView = (ImageView)view.findViewById(R.id.history_record);
		mHistoryView.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// to do
			}
		});
		return view;
	}
	
	public static SurveillanceFragment getInstance(){
		 if(mInstance == null){
			 mInstance = new SurveillanceFragment();
		 }
		 return mInstance;
	 }
	
	@Override
	public void setSurfaceSize(int width, int height, int visible_width,
			int visible_height, int sar_num, int sar_den) {
		mVideoWidth = width;
		mVideoHeight = height;
		mHandler.sendEmptyMessage(0);
	}

	public void onPause(){
		super.onPause();
		mCameraView.pause();
	}
	public void onResume(){
		super.onResume();
		mCameraView.resume();
	}
	public void onDestroy(){
		super.onDestroy();
		mCameraView.destroy();
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.surveilance:
			showSurveilance();
			break;
		case R.id.realtime:
			showRealtime();
			break;
		default:
			break;
		}
	}

	@SuppressLint("NewApi")
	private void showSurveilance(){
		if(!mShowSurveilance){
			jiankong_view.setVisibility(View.VISIBLE);
			mRealtimeLayout.setVisibility(View.GONE);
			mSurveilance.setBackgroundResource(R.drawable.slider);
			mRealtime.setBackground(null);
			mShowSurveilance = true;
		}
	}
	@SuppressLint("NewApi")
	private void showRealtime(){
		if(mShowSurveilance){
			jiankong_view.setVisibility(View.GONE);
			mRealtimeLayout.setVisibility(View.VISIBLE);
			mSurveilance.setBackground(null);
			mRealtime.setBackgroundResource(R.drawable.slider);
			mShowSurveilance = false;
		}
	}
}
